package com.css.fxfzfxqh.modules.lifelineEngineering.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.SmxVectorEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.SmxDataParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.repository.LifelineVectorRepository;
import com.css.fxfzfxqh.util.PlatformDateUtils;
import com.css.fxfzfxqh.util.PlatformSessionUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LifelineResultRepositoryImpl implements LifelineVectorRepository{

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public int getDataByVersion(String versionId,Integer type) {
        String sql = "";
        if(type == 15){
            sql = "select count(1) from fxqh_smx_vector_ts where version_id = '" + versionId + "'";
        }else {
            sql = "select count(1) from fxqh_smx_vector_base_station where version_id = '" + versionId + "'";
        }
        return jdbcTemplate.queryForObject(sql,int.class);
    }

    @Override
    public boolean save(SmxVectorEntity entity,Integer type,String table) {
        String sql = "insert into "+table+" values(:id,:vectorId,:versionId,:versionCode,:createUser,:createTime,:updateUser,:updateTime,:delFlag,ST_GeometryFromText(:geom,4490),:province,:city,:county,:fileName)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        return namedParameterJdbcTemplate.update(sql, source)>0;
    }

    @Override
    public void deleteByVersionId(String id,Integer type) {
        String table = "fxqh_smx_vector_base_station";
        if(type == 15){
            table = "fxqh_smx_vector_ts";
        }
        String sql = "update "+table+" set del_flag = '1' where id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public Map<String, Object> getByVersionAndType(SmxDataParam param) {
        HashMap<String, Object> map = new HashMap<>();
        Integer type = param.getType();
        Integer page = param.getPage();
        Integer size = param.getRows();
        String titleName = "fxqh_smx_vector_base_station";
        if(type == 15){
            titleName = "fxqh_smx_vector_ts";
        }
        StringBuilder sql = new StringBuilder("select * from "+titleName+" where version_id = '"+param.getVersionId()+"' and del_flag = '0'");
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        List<SmxVectorEntity> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SmxVectorEntity.class));
        map.put("rows", list);
        return map;
    }
}
